// pages/address/address.js
const app = getApp();
let Api = require('../../api/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressList:[],
        addressType:'',
        shareuserid: '', // 转发者id   礼物查询填写地址用到 
        ordernumber: '', // 订单号   礼物查询填写地址用到  
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let  self = this;
        if (options.addressType != undefined && options.addressType != null && options.addressType != '' && options.addressType == 'share'  ){
            self.setData({
                addressType: options.addressType,
                shareuserid: options.shareuserid,
                ordernumber: options.ordernumber,
            })
        }
        //地址列表展示接口
        Api.get(app.globalData.mallDomain, { 
            doShowAddressList: 1,
            token: app.globalData.token,
        }).then(res => {
            console.log(res,'地址列表')
            if (res.data.msg == '108') {
                if (res.data.list != '') {
                    let listData = [];
                    let defaultData = {};
                    for (let i = 0; i < res.data.list.length; i++) {
                        listData.push({
                            addressid: res.data.list[i].addressid,
                            name: res.data.list[i].name,
                            phone: res.data.list[i].phone,
                            area: res.data.list[i].area,
                            detaile: res.data.list[i].detaile,
                            isdefault: res.data.list[i].isdefault,
                        })
                    }
                    //默认地址放置在第一位
                    for (var i = 0; i < listData.length; i++) {
                        if (listData[i].isdefault == 1) {
                            defaultData = listData[i];
                            listData.splice(i, 1); // 如果数据组存在该元素，则把该元素删除
                            listData.unshift(defaultData); // 再添加到第一个位置
                        }
                    }
                    self.setData({
                        addressList: listData
                    })
                }
            }
           
        })
    },
    /**
     * 点击切换地址
     */
    switchAddress:function(e){
        let self = this;
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];  //上一个页面
        if (self.data.addressType == 'share'){
            Api.get(app.globalData.mallDomain, { //16）订单转赠填写信息
                doOrderGivingWritInfo: 1,
                token: app.globalData.token,
                shareuserid: self.data.shareuserid,
                ordernumber: self.data.ordernumber,
                addressid: e.currentTarget.dataset.addressid,
            }).then(res => {
                console.log(res,'订单转赠填写信息结果')
                if(res.data.msg == '115'){
                    self.setData({
                        tipText: '收货地址设置成功！等待发货',
                        showToast: true,
                    })
                    prevPage.setData({
                        consignee: self.data.addressList[e.currentTarget.dataset.index].name,
                        area: self.data.addressList[e.currentTarget.dataset.index].detaile,
                        phone: self.data.addressList[e.currentTarget.dataset.index].phone,
                        isReceiveOrder:'True',
                    })
                    setTimeout(function () {
                        self.setData({
                            tipText: '',
                            showToast: false
                        })
                        wx.navigateBack({
                            delta: 1
                        })
                    }, 1000)
                } else if (res.data.msg == '-115_2'){
                    self.setData({
                        tipText: '当前订单不是支付状态',
                        showToast: true,
                    })
                    setTimeout(function () {
                        self.setData({
                            tipText: '',
                            showToast: false
                        })
                    }, 1000)
                    return ;
                } else if (res.data.msg == '-115_3') {
                    self.setData({
                        tipText: '订单用户和转发用户不匹配',
                        showToast: true,
                    })
                    setTimeout(function () {
                        self.setData({
                            tipText: '',
                            showToast: false
                        })
                    }, 1000)
                    return;
                } else if (res.data.msg == '-115_1') {
                    self.setData({
                        tipText: '传入参数有误',
                        showToast: true,
                    })
                    setTimeout(function () {
                        self.setData({
                            tipText: '',
                            showToast: false
                        })
                    }, 1000)
                    return;
                } else if (res.data.msg == '-115_4'){
                    self.setData({
                        tipText: '该订单已被别人领取了！请点击返回按钮',
                        showToast: true,
                    })
                    setTimeout(function () {
                        self.setData({
                            tipText: '',
                            showToast: false
                        })
                    }, 1000)
                    return;
                }
            })
        }else{
            prevPage.setData({
                name: self.data.addressList[e.currentTarget.dataset.index].name,
                area: self.data.addressList[e.currentTarget.dataset.index].detaile,
                phone: self.data.addressList[e.currentTarget.dataset.index].phone,
                addressid: self.data.addressList[e.currentTarget.dataset.index].addressid,
            })
            wx.navigateBack({
                delta: 1
            })
        }
    },
    /**
     * 点击设置默认地址
     */
    radioChange: function (e) {
        let self = this;
        Api.get(app.globalData.mallDomain, { //12）设置默认地址
            doSetDefaultAddress: 1,
            token: app.globalData.token,
            addressid:e.detail.value
        }).then(res => {
            // let addresObj = {};
            // for (let i = 0; i < self.data.addressList.length; i ++){
            //     if (self.data.addressList[i].addressid == e.detail.value){
            //         addresObj = self.data.addressList[i]
            //     }
            // }
            // //修改订单列表属性值
            // let  pages = getCurrentPages();
            // let prevPage = pages[pages.length - 2];  //上一个页面
            // prevPage.setData({
            //     name: addresObj.name,
            //     area: addresObj.detaile,
            //     phone: addresObj.phone,
            // })
            console.log(res,'--设置默认地址---')
        })
    }, 
    /**
     * 删除地址
     */
    addressDelete: function(e) {
        let  self = this;
        let oIndex = e.currentTarget.dataset.index;
        let newArray = self.data.addressList;
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];  //上一个页面
        debugger
        if (newArray[oIndex].addressid == prevPage.data.addressid){
            prevPage.setData({
                name: '',
                phone: '',
                area: '',
                addressid: '',
            })
        } 
        newArray.splice(oIndex, 1)
        self.setData({
            addressList: newArray
        })
        Api.get(app.globalData.mallDomain, { //11）删除地址
            doDelAddress: 1,
            token: app.globalData.token,
            addressid: e.currentTarget.dataset.id
        }).then(res => {
            console.log(res,'-----删除地址--')
        })
    }, 
    /**
     * 添加地址
     */
    addAddress:function(){
        wx.navigateTo({
            url: '../addAddress/addAddress?type=add'
        })
    },
    /**
     * 编辑地址
     */
    addressEdit: function (e) {
        wx.navigateTo({
            url: '../addAddress/addAddress?type=edit&addressid=' + e.currentTarget.dataset.addressid + '&area=' + e.currentTarget.dataset.area + '&detaile=' + e.currentTarget.dataset.detaile + '&isdefault=' + e.currentTarget.dataset.isdefault + '&name=' + e.currentTarget.dataset.name + '&phone=' + e.currentTarget.dataset.phone
        })
    },
})